package com.neusoft.neuiotms.dcom.repository.insurance.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.enums.InsuranceReportTaskStatus;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceReportTask;
import com.neusoft.neuiotms.dcom.model.insurance.QInsuranceReportTask;
import com.neusoft.neuiotms.dcom.repository.insurance.CustomizedInsuranceReportTaskRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInsuranceReportTaskRepositoryImpl extends AbstractRepository
        implements CustomizedInsuranceReportTaskRepository {

    @Override
    public List<InsuranceReportTask> index(final Searchable searchable) {
        final QInsuranceReportTask qInsuranceReportTask = QInsuranceReportTask.insuranceReportTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InsuranceReportTask> query = this.query().selectFrom(qInsuranceReportTask);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<InsuranceReportTask> search(final Pageable pageable, final Searchable searchable) {
        final QInsuranceReportTask qInsuranceReportTask = QInsuranceReportTask.insuranceReportTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InsuranceReportTask> query = this.query().selectFrom(qInsuranceReportTask);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return InsuranceReportTask.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QInsuranceReportTask qInsuranceReportTask = QInsuranceReportTask.insuranceReportTask;

        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            // 编码检索
            where.and(super.equalsStr(qInsuranceReportTask.code, searchable, "code"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("reportPersonCode"))) {
            // 用户名
            where.and(super.equalsStr(qInsuranceReportTask.reportPersonCode, searchable, "reportPersonCode"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            // 状态检索
            where.and(super.equalsEnum(qInsuranceReportTask.status, searchable, "status",
                    value -> InsuranceReportTaskStatus.valueOf(value)));
        }

        if (null != searchable.getLongValue("startTime")) {
            where.and(super.beforeOrEqualsDate(qInsuranceReportTask.startTime, searchable, "startTime"));
        }

        if (null != searchable.getLongValue("endTime")) {
            where.and(super.afterOrEqualsDate(qInsuranceReportTask.endTime, searchable, "endTime"));
        }

        if (null != searchable.getStrValue("registerTaskId")) {
            // 总表任务id
            where.and(super.equalsLong(qInsuranceReportTask.registerTask.id, searchable, "registerTaskId"));
        }

        return where;
    }

}
